package com.tianyi.sfzy1.controller;

import com.github.pagehelper.PageInfo;
import com.tianyi.sfzy1.domain.Privatemessage;
import com.tianyi.sfzy1.service.PrivatemessageService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/privatemessage")
public class PrivatemessageController {
    @Autowired
    private PrivatemessageService privatemessageService;
    //插入用户提问的问题和相关信息
    @PostMapping("/insertQuestion")
    public R insertQuestion(Privatemessage privatemessage){
        int i = privatemessageService.insertQuestion(privatemessage);
        if (i>0){
            return R.ok();
        }
        return R.fail("发送私信失败");
    }

    //根据会员编号和咨询师编号查询私信列表
    @GetMapping("/selectMessage")
    public R selectMessage(int consultantId){
        List<Privatemessage> privatemessages = privatemessageService.selectMessage(consultantId);
        if (privatemessages!=null){
            return R.ok(privatemessages);
        }
        return R.fail("查询失败");
    }

    //根据咨询师编号查询私信列表
    @GetMapping("/select")
    public R selectById(int consultantId){
        List<Privatemessage> privatemessages = privatemessageService.selectById(consultantId);
        if (privatemessages!=null){
            return R.ok(privatemessages);
        }
        return R.fail("查询失败");
    }

    //根据咨询师编号查询私信列表(分页)
    @GetMapping("/select2")
    public R selectById2(int consultantId,int pageNum,int pageSize){
        PageInfo<Privatemessage> pageInfo = privatemessageService.selectByIdPage2(consultantId,pageNum,pageSize);
        if (pageInfo!=null){
            return R.ok(pageInfo);
        }
        return R.fail("查询失败");
    }

    //根据id修改回复内容
    @PutMapping("/updateAnswer")
    public R updateAnswer(int messageId,String replyContent){
        int i = privatemessageService.updateAnswer(messageId,replyContent);
        if (i>0){
            return R.ok();
        }
        return R.fail("回复失败");
    }

    //根据id删除私信
    @DeleteMapping("/deleteMessage")
    public R deleteMessage(int messageId){
        int i = privatemessageService.deleteMessage(messageId);
        if (i>0){
            return R.ok();
        }
        return R.fail("删除失败");
    }

    //根据用户姓名和咨询师id查询私信列表(模糊查询)
    @GetMapping("/selectByUsername666")
    public R selectByUsername666(String realname,int consultantId){
        List<Privatemessage> privatemessages = privatemessageService.selectByUsername666(realname,consultantId);
        if (privatemessages!=null){
            return R.ok(privatemessages);
        }
        return R.fail("查询失败");
    }

    //根据私信id删除私信信息(批量删除)
    @DeleteMapping("/deleteMessageByIds")
    public R deleteMessageByIds(@RequestBody List<Integer> messageId){
        int i = privatemessageService.deleteMessageByIds(messageId);
        if (i>0){
            return R.ok();
        }
        return R.fail("删除失败");
    }

    //根据咨询师id查询私信的数量
    @GetMapping("/selectCount2")
    public R selectCount2(int consultantId){
        int i = privatemessageService.selectCount2(consultantId);
        if (i>0){
            return R.ok(i);
        }
        return R.fail("查询失败");
    }
}
